<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Yanhao extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'UserId'        => $bean->getSupplierNum(), //商户号
            'TimesTamp'     => millisecondTimestamp(),
            'ProductNumber' => $bean->getGoodsId(), //商品编码
            'Count'         => $bean->getRechargeNum() * $bean->getStartCharging(),//充值数量
            'UserOrderId'   => $bean->getOrderNo(), //订单号
            'Account'       => $bean->getAccount(),
            'ClientIP'      => $bean->getIp(),
            'NotifyUrl'     => $bean->getNotifyUrl(), //异步回调地址
        ];
        ksort($array);
        $md5_str = $array['UserId'].$array['Count'].$array['NotifyUrl'].$array['Account'].$array['ClientIP']
            .$array['UserOrderId'].$array['ProductNumber'].$array['TimesTamp']. $bean->getSecretKey();
        $str = md5($md5_str);
        $sign = strtoupper($str);
        $array['sign'] = $sign;

        return $array;
    }


    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('Status', '');
        $order_sn = $request->input('UserOrderId', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, [2, 3]))->setRechargeSuccess($status == 2);
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response()->json(['code' => 0]);
    }
}
